Install necessary files

In [ ]:
# import packages for analysis and modeling
!pip install fiona
!pip install geopandas
import pandas as pd #data frame operations
import numpy as np #arrays and math functions
from scipy.stats import uniform #for training and test splits
import statsmodels.api as smf #R-like model specification
import matplotlib.pyplot as plt #2D plotting
import requests
import folium
import sys
import math
from folium.plugins import FeatureGroupSubGroup
from folium.plugins import MarkerCluster
import geopandas as gpd
from shapely.geometry import Point
from folium.plugins import MeasureControl
from datetime import datetime
from folium.plugins import TimestampedGeoJson
Collecting fiona
  Downloading https://files.pythonhosted.org/packages/37/94/4910fd55246c1d963727b03885ead6ef1cd3748a465f7b0239ab25dfc9a3/Fiona-1.8.18-cp36-cp36m-manylinux1_x86_64.whl (14.8MB)
     |████████████████████████████████| 14.8MB 318kB/s 
Requirement already satisfied: attrs>=17 in /usr/local/lib/python3.6/dist-packages (from fiona) (20.3.0)
Collecting cligj>=0.5
  Downloading https://files.pythonhosted.org/packages/42/1e/947eadf10d6804bf276eb8a038bd5307996dceaaa41cfd21b7a15ec62f5d/cligj-0.7.1-py3-none-any.whl
Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from fiona) (2020.12.5)
Requirement already satisfied: six>=1.7 in /usr/local/lib/python3.6/dist-packages (from fiona) (1.15.0)
Collecting munch
  Downloading https://files.pythonhosted.org/packages/cc/ab/85d8da5c9a45e072301beb37ad7f833cd344e04c817d97e0cc75681d248f/munch-2.5.0-py2.py3-none-any.whl
Requirement already satisfied: click<8,>=4.0 in /usr/local/lib/python3.6/dist-packages (from fiona) (7.1.2)
Collecting click-plugins>=1.0
  Downloading https://files.pythonhosted.org/packages/e9/da/824b92d9942f4e472702488857914bdd50f73021efea15b4cad9aca8ecef/click_plugins-1.1.1-py2.py3-none-any.whl
Installing collected packages: cligj, munch, click-plugins, fiona
Successfully installed click-plugins-1.1.1 cligj-0.7.1 fiona-1.8.18 munch-2.5.0
Collecting geopandas
  Downloading https://files.pythonhosted.org/packages/2a/9f/e8a440a993e024c0d3d4e5c7d3346367c50c9a1a3d735caf5ee3bde0aab1/geopandas-0.8.2-py2.py3-none-any.whl (962kB)
     |████████████████████████████████| 972kB 11.1MB/s 
Collecting pyproj>=2.2.0
  Downloading https://files.pythonhosted.org/packages/e4/ab/280e80a67cfc109d15428c0ec56391fc03a65857b7727cf4e6e6f99a4204/pyproj-3.0.0.post1-cp36-cp36m-manylinux2010_x86_64.whl (6.4MB)
     |████████████████████████████████| 6.5MB 30.3MB/s 
Requirement already satisfied: pandas>=0.23.0 in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.1.5)
Requirement already satisfied: shapely in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.7.1)
Requirement already satisfied: fiona in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.8.18)
Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from pyproj>=2.2.0->geopandas) (2020.12.5)
Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (1.19.5)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (2018.9)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (2.8.1)
Requirement already satisfied: attrs>=17 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (20.3.0)
Requirement already satisfied: six>=1.7 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (1.15.0)
Requirement already satisfied: click<8,>=4.0 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (7.1.2)
Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (1.1.1)
Requirement already satisfied: munch in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (2.5.0)
Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (0.7.1)
Installing collected packages: pyproj, geopandas
Successfully installed geopandas-0.8.2 pyproj-3.0.0.post1
/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm

Pull in and clean dataframes

In [ ]:
albania = pd.read_csv("/content/drive/MyDrive/albania/conflict_data_alb.csv")
albania.rename(columns=albania.iloc[0]).drop(albania.index[0])
albania.columns=['data.id', 'ISO', 'event.code', 'event.id.city', 'event_date', 'event_year', 'timeprecision', 'event_type', 'subeventtype', 'actor1', 'assoc.actor1', 'inter1', 'actor1', 'assoc2','inter2','interaction', 'region', 'country', 'admin1', 'admin2', 'admin3', 'loc.name', 'latitude', 'longitude', 'geoprecision', 'source', 'source.scale', 'notes', 'fatalities', 'timestamp', 'countrycode']
albania=albania.drop(albania.index[[0]])
albania=albania[['event_date', 'event_year', 'event_type', 'subeventtype', 'actor1', 'assoc.actor1', 'actor1', 'assoc2', 'admin1', 'admin2', 'admin3', 'loc.name', 'latitude', 'longitude', 'source', 'source.scale', 'notes']]
albania['longitude']=albania['longitude'].astype(float)
albania['latitude']=albania['latitude'].astype(float)
x=np.unique(albania.event_type)
#albania.info()
#albania.head()
viol=albania[~albania["event_type"].isin(["Strategic developments", "Protests"])]
peace=albania[albania["event_type"].isin(["Strategic developments", "Protests"])]
albania["event_date"]=pd.to_datetime(albania["event_date"])
#albania['event_date'] = albania['event_date'].dt.strftime('%Y-%m-%d')
twenty=albania[albania["event_year"]=="2020"]
twenty1=albania[albania["event_year"]=="2021"]
ninteen=albania[albania["event_year"]=="2019"]
eighteen=albania[albania["event_year"]=="2018"]
albania.info()
albania.head()
albania.to_csv(r'albania.csv', index = False)
<class 'pandas.core.frame.DataFrame'>
Int64Index: 825 entries, 1 to 825
Data columns (total 19 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   event_date    825 non-null    datetime64[ns]
 1   event_year    825 non-null    object        
 2   event_type    825 non-null    object        
 3   subeventtype  825 non-null    object        
 4   actor1        825 non-null    object        
 5   actor1        140 non-null    object        
 6   assoc.actor1  507 non-null    object        
 7   actor1        825 non-null    object        
 8   actor1        140 non-null    object        
 9   assoc2        35 non-null     object        
 10  admin1        825 non-null    object        
 11  admin2        825 non-null    object        
 12  admin3        1 non-null      object        
 13  loc.name      825 non-null    object        
 14  latitude      825 non-null    float64       
 15  longitude     825 non-null    float64       
 16  source        825 non-null    object        
 17  source.scale  825 non-null    object        
 18  notes         825 non-null    object        
dtypes: datetime64[ns](1), float64(2), object(16)
memory usage: 128.9+ KB

Create a Map to demonstrate Albanian Sigacts with a switch for Violent and Non Violent Events.

In [ ]:
eventmap = folium.Map(
    location=[41.00, 20.00],
    zoom_start=8,
    tiles='cartodbdark_matter'
)


mcg=folium.plugins.MarkerCluster(name="Event Type", control=False)
eventmap.add_child(mcg)

#fg2=folium.plugins.MarkerCluster(name="Year", control=False)
#eventmap.add_child(fg2)

def color(event):
  if event == "Protests": 
    col='blue'
  elif event=="Strategic developments":
    col='green'
  else:
    col='red'
  return col

def icon(event):
  if event == "Protests": 
    icon='fa-bullhorn'
  elif event=="Strategic developments":
    icon='fa-bullseye'
  else:
    icon='fa-bomb'
  return icon

marker_cluster=folium.plugins.FeatureGroupSubGroup(mcg, 'Violence')
eventmap.add_child(marker_cluster)
marker_cluster1=folium.plugins.FeatureGroupSubGroup(mcg, 'Nonviolence')
eventmap.add_child(marker_cluster1)
'''marker_cluster2=folium.plugins.FeatureGroupSubGroup(mcg, '2020')
eventmap.add_child(marker_cluster2)
marker_cluster3=folium.plugins.FeatureGroupSubGroup(mcg, '2021')
eventmap.add_child(marker_cluster3)
marker_cluster4=folium.plugins.FeatureGroupSubGroup(mcg, '2018')
eventmap.add_child(marker_cluster4)
marker_cluster5=folium.plugins.FeatureGroupSubGroup(mcg, '2019')
eventmap.add_child(marker_cluster5)'''
for _, row in viol.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster)


for _, row in peace.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster1)

'''for _, row in twenty.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster2)'''


'''for _, row in twenty1.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster3)'''

'''for _, row in eighteen.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster4)'''


'''for _, row in ninteen.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster5)'''            
folium.TileLayer("OpenStreetMap").add_to(eventmap)
folium.LayerControl().add_to(eventmap)
eventmap
$ jupyter trust mynotebook.ipynb
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Created a similar map for exploring the data with a filter for year of event occurence.

In [ ]:
eventmap2 = folium.Map(
    location=[41.00, 20.00],
    zoom_start=8,
    tiles='OpenStreetMap'
)


mcg=folium.plugins.MarkerCluster(name="Event Type", control=False)
eventmap2.add_child(mcg)

#fg2=folium.plugins.MarkerCluster(name="Year", control=False)
#eventmap.add_child(fg2)

def color(event):
  if event == "Protests": 
    col='blue'
  elif event=="Strategic developments":
    col='green'
  else:
    col='red'
  return col

def icon(event):
  if event == "Protests": 
    icon='fa-bullhorn'
  elif event=="Strategic developments":
    icon='fa-bullseye'
  else:
    icon='fa-bomb'
  return icon

'''marker_cluster=folium.plugins.FeatureGroupSubGroup(mcg, 'Violence')
eventmap.add_child(marker_cluster)
marker_cluster1=folium.plugins.FeatureGroupSubGroup(mcg, 'Nonviolence')
eventmap.add_child(marker_cluster1)'''
marker_cluster2=folium.plugins.FeatureGroupSubGroup(mcg, '2020')
eventmap2.add_child(marker_cluster2)
marker_cluster3=folium.plugins.FeatureGroupSubGroup(mcg, '2021')
eventmap2.add_child(marker_cluster3)
marker_cluster4=folium.plugins.FeatureGroupSubGroup(mcg, '2018')
eventmap2.add_child(marker_cluster4)
marker_cluster5=folium.plugins.FeatureGroupSubGroup(mcg, '2019')
eventmap2.add_child(marker_cluster5)
'''for _, row in viol.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster)'''


'''for _, row in peace.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster1)'''

for _, row in twenty.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster2)


for _, row in twenty1.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster3)

for _, row in eighteen.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster4)


for _, row in ninteen.iterrows():
  html="""<i>Location: </i> <br> <b>{}</b> <br> 
                  <i>Date: </i><b><br>{}</b><br>
                  <i>Description: </i><b><br>{}</b><br>""".format(
                    row['loc.name'], 
                    row['event_date'], 
                    row['notes'])
  iframe=folium.IFrame(html, width=200, height=300)
  popup = folium.Popup(iframe,
                     max_width=500)
  marker=folium.Marker(
        location=[row['latitude'], row['longitude']],
        popup=popup,
        icon=folium.Icon(color=color(row['event_type']), icon=icon(row['event_type']), prefix='fa', icon_color='black')).add_to(marker_cluster5)            
folium.TileLayer("cartodbdark_matter").add_to(eventmap2)
folium.LayerControl().add_to(eventmap2)
eventmap2
$ jupyter trust mynotebook.ipynb
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook